﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;

namespace TimeTravel.Commons
{
    public class DateHelper
    {
        /// <summary>
        /// 时间戳转为C#格式时间
        /// </summary>
        /// <returns></returns>
        public static DateTime GetDateFromTimeStamp(int timeStamp)
        {
            var dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            var lTime = long.Parse(timeStamp + "0000000");
            var toNow = new TimeSpan(lTime); 
            return dtStart.Add(toNow);
        }

        /// <summary>
        /// DateTime时间格式转换为Unix时间戳格式
        /// </summary>
        /// <returns></returns>
        public static int GetTimeStampFromDate(DateTime time)
        {
            var startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            return (int) ((time - startTime).TotalSeconds);
        }
    }
}
